package com.jza04.flightbooking.service.impl;

import com.jza04.flightbooking.entity.Flight;
import com.jza04.flightbooking.mapper.FlightMapper;
import com.jza04.flightbooking.service.FlightService;
import com.jza04.flightbooking.service.FlightTicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 航班服务实现类
 */
@Service
public class FlightServiceImpl implements FlightService {

    @Autowired
    private FlightMapper flightMapper;
    
    @Autowired
    private FlightTicketService flightTicketService;
    
    @Override
    public List<Flight> findAllFlights() {
        return flightMapper.findAll();
    }
    
    @Override
    public Flight findFlightById(String flightId) {
        return flightMapper.findById(flightId);
    }
    
    @Override
    public List<Flight> findFlightsByRoute(String departureCity, String arrivalCity) {
        return flightMapper.findByRoute(departureCity, arrivalCity);
    }
    
    @Override
    @Transactional
    public boolean addFlight(Flight flight) {
        return flightMapper.insert(flight) > 0;
    }
    
    @Override
    @Transactional
    public boolean updateFlight(Flight flight) {
        return flightMapper.update(flight) > 0;
    }
    
    @Override
    @Transactional
    public boolean deleteFlight(String flightId) {
        // 取消与航班相关的所有机票
        flightTicketService.cancelTicketsByFlightId(flightId);
        
        // 删除航班
        return flightMapper.delete(flightId) > 0;
    }
    
    @Override
    @Transactional
    public boolean decreaseRemainingSeats(String flightId) {
        return flightTicketService.decreaseRemainingSeats(flightId);
    }
    
    @Override
    @Transactional
    public boolean increaseRemainingSeats(String flightId) {
        return flightTicketService.increaseRemainingSeats(flightId);
    }
}